package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.WorkOrder;
import cn.net.shuxing.yifu.common.dto.CommitOrderReq;
import cn.net.shuxing.yifu.common.dto.CommonOrderResp;
import cn.net.shuxing.yifu.common.dto.HistoryWorkOrderResp;
import cn.net.shuxing.yifu.common.mapper.WorkOrderMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class WorkOrderService extends BaseService<WorkOrder, WorkOrderMapper> {

    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private OssCommonService ossCommonService;

    public List<WorkOrder> getsByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(WorkOrder.class);
        example.equals("userId", userId);
        example.setOrderByClause("'createTime' DESC");
        return getsByExample(example);
    }

    public List<HistoryWorkOrderResp> getHistoryOrder(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        WorkOrder workOrderBean = new WorkOrder();
        workOrderBean.clearCreateTimeAndUpdateTime();
        workOrderBean.setUserId(userId);
        List<WorkOrder> list = gets(workOrderBean);
        List<HistoryWorkOrderResp> historyWorkOrderResps = new ArrayList<>();
        for (WorkOrder workOrder : list) {
            HistoryWorkOrderResp historyWorkOrderResp = new HistoryWorkOrderResp();
            BeanUtils.copyProperties(workOrder, historyWorkOrderResp);
            historyWorkOrderResps.add(historyWorkOrderResp);
        }
        return historyWorkOrderResps;
    }

    public List<CommonOrderResp> getCommonOrderResp() {
        CommonExample example = new CommonExample(WorkOrder.class);
        example.equals("type", 3);
        List<WorkOrder> workOrders = getsByExample(example);
        List<CommonOrderResp> commonOrderRespList = new ArrayList<>();
        for (WorkOrder workOrder : workOrders) {
            CommonOrderResp commonOrderResp = new CommonOrderResp();
            BeanUtils.copyProperties(workOrder, commonOrderResp);
            commonOrderRespList.add(commonOrderResp);
        }
        return commonOrderRespList;
    }

    public void commitOrder(CommitOrderReq commitOrderReq) {
        WorkOrder workOrder = new WorkOrder();
        BeanUtils.copyProperties(commitOrderReq, workOrder);
        workOrder.setCoinName(coinTypeService.getByPrimaryKey(workOrder.getCoinType()).getName());
        if (commitOrderReq.getPhotoUrls() != null) {
            String[] photoUrls = commitOrderReq.getPhotoUrls().split(",");
            for (int i = 0; i < photoUrls.length; i++) {
                if (i == 0) {
                    workOrder.setPhoto1Url(photoUrls[i]);
                    continue;
                }
                if (i == 1) {
                    workOrder.setPhoto2Url(photoUrls[i]);
                    continue;
                }
                if (i == 2) {
                    workOrder.setPhoto3Url(photoUrls[i]);
                    continue;
                }
            }
        }
        workOrder.setStatus(2);
        insert(workOrder);
    }

}